﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Microsoft.SharePoint;

namespace TST.SharePointObjects
{
    public class SiteInfoStore
    {
        private Guid _site;
        private Guid _web;
        private Guid _webApplicationId;
        private Guid _farmId;
        private string _title;
        private string _url;
        private string _siteTitle;
        private string _siteUrl;
        private DateTime _indexed;
        private SharePointObjectsDatabase _database;

        public SiteInfoStore(Guid siteID, Guid webID, Guid webApplicationId, Guid farmId, SharePointObjectsDatabase database)
        {
            _site = siteID;
            _web = webID;
            _indexed = DateTime.MinValue;
            _webApplicationId = webApplicationId;
            _farmId = farmId;
            _database = database;
        }

        public SiteInfoStore(Guid siteID, Guid webID, Guid webApplicationId, Guid farmId, string title, string url, string siteCollectionTitle, string siteCollectionUrl, SharePointObjectsDatabase database)
        {
            _site = siteID;
            _web = webID;
            _title = title;
            _url = url;
            _siteUrl = siteCollectionUrl;
            _siteTitle = siteCollectionTitle;
            _webApplicationId = webApplicationId;
            _farmId = farmId;
            _database = database;
        }

        public string Title
        {
            get
            {
                if (string.IsNullOrEmpty(_title))
                    GetSiteInfo();
                return _title;
            }
        }

        public string Url
        {
            get
            {
                if (string.IsNullOrEmpty(_url))
                    GetSiteInfo();
                return _url;
            }
        }

        public string SiteCollectionTitle
        {
            get
            {
                if (string.IsNullOrEmpty(_siteTitle))
                    GetSiteInfo();
                return _siteTitle;
            }
        }

        public DateTime Indexed
        {
            get
            {
                if (_indexed == null)
                    GetSiteInfo();
                return _indexed;
            }
            set
            {
                _indexed = value;
            }
        }

        public string SiteCollectionUrl
        {
            get
            {
                if (string.IsNullOrEmpty(_siteUrl))
                    GetSiteInfo();
                return _siteUrl;
            }
        }

        public bool GetInfo()
        {
            _indexed = DateTime.MinValue;
            GetSiteInfo();
            return _indexed > DateTime.MinValue;
        }

        private void GetSiteInfo()
        {
            DataTable data = _database.GetData(GetSelectProcedure(), CommandType.StoredProcedure);
            if (data.Rows.Count == 1)
            {
                DataRow row = data.Rows[0];
                _title = row["Title"].ToString();
                _url = row["Url"].ToString();
                _siteTitle = row["SiteCollectionTitle"].ToString();
                _siteUrl = row["SiteCollectionUrl"].ToString();
                if (row["IndexDate"].GetType() == typeof(System.DBNull))
                    _indexed = DateTime.MinValue;
                else
                    _indexed = (DateTime)row["IndexDate"];
            }
        }

        private SqlCommand GetUpdateProcedure()
        {
            // Setup the INSERT command
            SqlCommand updateProcedure = new SqlCommand();
            updateProcedure.CommandType = CommandType.StoredProcedure;
            updateProcedure.UpdatedRowSource = UpdateRowSource.None;
            updateProcedure.CommandText = "UpdateSiteInfo";

            SqlParameter insertParamWebApplicationID = new SqlParameter("@WebApplicationID", SqlDbType.UniqueIdentifier);
            insertParamWebApplicationID.Value = _webApplicationId;
            updateProcedure.Parameters.Add(insertParamWebApplicationID);

            SqlParameter insertParamFarmID = new SqlParameter("@FarmID", SqlDbType.UniqueIdentifier);
            insertParamFarmID.Value = _farmId;
            updateProcedure.Parameters.Add(insertParamFarmID);

            SqlParameter insertParamSiteID = new SqlParameter("@SiteID", SqlDbType.UniqueIdentifier);
            insertParamSiteID.Value = _site;
            updateProcedure.Parameters.Add(insertParamSiteID);

            SqlParameter insertParamWebID = new SqlParameter("@WebID", SqlDbType.UniqueIdentifier);
            insertParamWebID.Value = _web;
            updateProcedure.Parameters.Add(insertParamWebID);

            SqlParameter insertParamName = new SqlParameter("@Title", SqlDbType.NVarChar, 255);
            insertParamName.Value = Title;
            updateProcedure.Parameters.Add(insertParamName);

            SqlParameter insertParamUrl = new SqlParameter("@Url", SqlDbType.NVarChar, 255);
            insertParamUrl.Value = Url;
            updateProcedure.Parameters.Add(insertParamUrl);

            SqlParameter paramSiteCollectionTitle = new SqlParameter("@SiteCollectionTitle", SqlDbType.NVarChar, 255);
            paramSiteCollectionTitle.Value = SiteCollectionTitle;
            updateProcedure.Parameters.Add(paramSiteCollectionTitle);

            SqlParameter paramSiteCollectionUrl = new SqlParameter("@SiteCollectionUrl", SqlDbType.NVarChar, 255);
            paramSiteCollectionUrl.Value = SiteCollectionUrl;
            updateProcedure.Parameters.Add(paramSiteCollectionUrl);

            SqlParameter paramIndexDate = new SqlParameter("@IndexDate", SqlDbType.DateTime);
            if (Indexed==DateTime.MinValue)
                paramIndexDate.Value = System.DBNull.Value;
            else
                paramIndexDate.Value = Indexed;
            updateProcedure.Parameters.Add(paramIndexDate);

            return updateProcedure;
        }

        private SqlCommand GetDeleteCommand()
        {
            // Setup the delete command
            SqlCommand deleteCommand = new SqlCommand();
            deleteCommand.CommandType = CommandType.Text;
            deleteCommand.UpdatedRowSource = UpdateRowSource.None;
            deleteCommand.CommandText = string.Format("delete from SiteInfo where SiteID='{0}' and WebID='{1}'", _site, _web);

            return deleteCommand;
        }

        private SqlCommand GetSelectProcedure()
        {
            SqlCommand selectCommand = new SqlCommand();
            selectCommand.CommandText = "GetSiteInfo";

            SqlParameter paramWebApplicationID = new SqlParameter("@WebApplicationID", SqlDbType.UniqueIdentifier);
            paramWebApplicationID.Value = _webApplicationId;
            selectCommand.Parameters.Add(paramWebApplicationID);

            SqlParameter paramFarmID = new SqlParameter("@FarmID", SqlDbType.UniqueIdentifier);
            paramFarmID.Value = _farmId;
            selectCommand.Parameters.Add(paramFarmID);

            SqlParameter paramSiteID = new SqlParameter("@SiteID", SqlDbType.UniqueIdentifier);
            paramSiteID.Value = _site;
            selectCommand.Parameters.Add(paramSiteID);

            SqlParameter paramWebID = new SqlParameter("@WebID", SqlDbType.UniqueIdentifier);
            paramWebID.Value = _web;
            selectCommand.Parameters.Add(paramWebID);

            return selectCommand;
        }

        public void SaveSiteInfo()
        {
            _database.RunStoredProcedure(GetUpdateProcedure());
        }


        internal static void IndexComplete(SPSite site, SPWeb web, SharePointObjectsDatabase database)
        {
            using (SPWeb rootWeb = site.RootWeb)
            {
                SiteInfoStore info = new SiteInfoStore(site.ID,
                    web.ID,
                    site.WebApplication.Id,
                    site.WebApplication.Farm.Id,
                    web.Title,
                    web.Url,
                    rootWeb.Title,
                    rootWeb.Url,
                    database);
                info.Indexed = DateTime.Now;
                info.SaveSiteInfo();
            }
        }
    }
}
